/**
* Proyecto: Cognitum
* Global IT Services 2008
* @author Esanantonio
*/

Cognitum.Cobranza.Factura.Editor = Ext.extend(Karma.EBCard, {
    initComponent: function() {
        var _sections = [
        {
            title: 'FacturaSap',
            propertyName: 'FacturaSap',
            items: [{
                fieldLabel: 'Folio',
                name: 'Folio',
                readOnly: true
            }, {
                fieldLabel: 'Id',
                name: 'Id',
                xtype: 'hidden'
            }, {
                fieldLabel: 'Fecha',
                name: 'FechaDocmento',
                xtype: 'datefield',
                readOnly: true
            }, {
                fieldLabel: 'R.F.C.',
                name: 'Rfc',
                readOnly: true
            }, {
                fieldLabel: 'Cliente',
                name: 'NombreCliente',
                readOnly: true
            }, {
                fieldLabel: 'Direccion',
                name: 'Direccion',
                readOnly: true
            }, {
                fieldLabel: 'Sub Total',
                name: 'SubTotal',
                readOnly: true
            }, {
                fieldLabel: '% I.V.A.',
                name: 'PorcentajeIva',
                readOnly: true
            }, {
                fieldLabel: 'I.V.A.',
                name: 'IvaTotal',
                readOnly: true
            }, {
                fieldLabel: 'Total',
                name: 'TotalFactura',
                readOnly: true
            }, {
                fieldLabel: 'Estatus Factura',
                name: 'EstatusFactura',
                readOnly: true
            }]
        }, {
            title: 'Generales',
            items: [{
                fieldLabel: 'Folio',
                name: 'Folio',
                readOnly: true
            }]
        }, {
            title: 'Documento Factura',
            items: [{
                fieldLabel: 'Documento Factura',
                hideLabel: true,
                name: 'DocumentoFactura',
                entityName: 'DocumentosDigitalizadosFactura',
                xtype: 'ag.list',
                context: 'd.Factura.Id=?',
                anchor: '99% 99%'
            }]
        }, {
           title: 'Conceptos Factura',
           items: [
            {
                canNew: false,
                canDelete: false,
                hideLabel: true,
                name: 'ConceptosFactura',
                entityName: 'ConceptosFactura',
                xtype: 'ag.list',
                selectionConfig: {
                    multiSelect: true,
                    width: 700
                },
                context: 'f.Key.DocEntry=? ',
                anchor: '100% 100%'
             }]
         }, {
            xtype: 'sec.editor',
            title: 'Seguridad'
         }, {
            xtype: 'aud.editor',
            title: 'Auditoria'
         }];

                Ext.apply(this, {
                    sections: _sections
                });

                this.addEvents({
                    'generarFacturaElectronica': true
                });

                Cognitum.Cobranza.Factura.Editor.superclass.initComponent.apply(this, arguments);
    },

    getTBarCustomControls: function() {
        return [this.generearFacturaElectronicaBtn = new Ext.Button({
            text: 'Generar Factura Electronica',
            handler: this.onGenerarFacturaElectronica,
            scope: this
        })];
    },

    puedeGenerarFacturaElectronica: function(value) {

        if (!this.entity.security.has('generarFacturaElectronica')) {
            return false;
        }

        return (value.FacturaSap.EstatusFactura == 'Vigente');

    },

    updateControls: function(value) {

        Cognitum.Cobranza.Factura.Editor.superclass.updateControls.apply(this, arguments);
        this.generearFacturaElectronicaBtn.setDisabled(!this.puedeGenerarFacturaElectronica(value));

    },

    onGenerarFacturaElectronica: function() {
        if (PLOG.isDebugEnabled()) {
            PLOG.debug('[EditorBase.onGenerarFacturaElectronica] -');
        }
        Ext.Msg.show({
            title: 'Factura Electronica',
            msg: 'Esta apunto de generar una Factura Electronica, Desea continuar?',
            buttons: Ext.Msg.YESNO,
            fn: function(result) {
                if (result === 'yes') {
                    this.entity.service = 'Cognitum.Dominio.Cobranza.IFacturaService';
                    var facturaId = this.getForm().findField('Id').getValue();
                    var facturaFolio = this.getForm().findField('Folio').getValue();
                    var facturaSapId = this.getForm().findField('FacturaSap.Id').getValue();

                    this.doOperationWithParams('DoGenerarFacturaElectronica', facturaSapId, {
                        fn: function(factura) {
                            this.status.setStatus({
                                text: 'La Factura Electronica fue generada existosamente',
                                iconCls: 'ok-icon'
                            });
                            this.doClose();
                            Ext.MessageBox.alert('Exito', 'La operacion Generar Factura Electronica se realizo con exito', function() {
                                this.entity.onOpen(factura.Id);
                            }, this);
                        },
                        scope: this
                    });
                }
            },
            animEl: 'elId',
            icon: Ext.MessageBox.QUESTION,
            scope: this
        });
    }

});

Ext.reg('factura.editor', Cognitum.Cobranza.Factura.Editor);